package com.muki.spider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

import com.muki.corpus.Corpus;
import com.muki.corpus.CorpusWriting;

/**
 * 爬虫类
 * @author ljh_2017 
 *
 */
public class MukiSpider implements Spider{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final Corpus cps = new Corpus();
	
	private final CorpusWriting cw = new CorpusWriting();
	
	private String topic;

	private String name;
	
	private String url=null;
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	//正在表达式
	private String regx;
	
	private int count=0;

	//爬虫状态
	private boolean status;

	//爬虫的优先级别
	private int priority;

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		if(priority==0)
			this.priority = priority;
	}

	@Override
	public void crawling(String uri) {
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(uri);
		if(uri==null)
			return;
		try {
			int code = client.executeMethod(method);
			if(code==HttpStatus.SC_OK) {
				BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));  
				StringBuffer stringBuffer = new StringBuffer();  
				String str = "";  
				while((str = reader.readLine())!=null){  
				    stringBuffer.append(str);  
				}  
				String result = stringBuffer.toString();
				count++;
				cps.parse(result);
				cw.createFile(topic, uri, result);
				reader.close();
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	@Override
	public void bind(String topic) {
		this.topic = topic;
		/**
		 * 绑定corpus
		 */
		cps.sendCorpus(topic);
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	@Override public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRegx() {
		return regx;
	}

	public void setRegx(String regx) {
		this.regx = regx;
	}
	
	public int getCount() {
		return count;
	}

	@Override
	public boolean getStatus() {
		return status;
	}

}
